package com.itgone.presenter.impl;

import android.content.Intent;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.utils.LogUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.itgone.bean.PageInfo;
import com.itgone.bean.Product;
import com.itgone.common.ServerResponse;
import com.itgone.model.IProductModel;
import com.itgone.model.impl.ProductModelImpl;
import com.itgone.presenter.IProductListPresenter;
import com.itgone.util.AppUtil;
import com.itgone.util.JsonUtil;
import com.itgone.view.IProductListView;
import com.itgone.view.activity.ProductDetailActivity;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

import static com.itgone.common.Const.KEY_DETAIL;
import static com.itgone.common.Const.KEY_MAIN_IMAGE;
import static com.itgone.common.Const.KEY_NAME;
import static com.itgone.common.Const.KEY_PRODUCT_ID;

public class ProductListPresenterImpl extends XRefreshView.SimpleXRefreshListener implements IProductListPresenter {
    private int pageNum = 1;
    private boolean isHasNextPage;

    private final IProductListView iProductListView;
    private IProductModel iProductModel = new ProductModelImpl();


    public ProductListPresenterImpl(IProductListView iProductListView) {
        this.iProductListView = iProductListView;
    }

    @Override
    public void listProduct(String categoryId, String keyword) {
        iProductModel.list(categoryId, keyword, String.valueOf(pageNum), null, new StringCallback() {

            @Override
            public void onAfter(int id) {
                iProductListView.stopLoadMore();
                iProductListView.stopRefresh();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                LogUtils.e("", e);
            }

            @Override
            public void onResponse(String response, int id) {
                ServerResponse serverResponse = JsonUtil.parseResponsePageList(response, Product.class);
                if (serverResponse.isSuccess()) {
                    PageInfo pageInfo = (PageInfo) serverResponse.getData();
                    List<Product> list = (List<Product>) pageInfo.getList();

                    List<Product> productList = iProductListView.getProductList();
                    if (productList == null) {
                        productList = list;
                    } else {
                        productList.addAll(productList.size(), list);
                    }
                    iProductListView.notifyProductList(productList);

                    isHasNextPage = pageInfo.isHasNextPage();

                } else {
                    LogUtils.e(serverResponse.toString());
                    ToastUtils.showToast(AppUtil.getContext(), serverResponse.getMsg());
                }
            }
        });
    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return true;
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(AppUtil.getContext(), ProductDetailActivity.class);
        Product product = iProductListView.getProductList().get(position);
        intent.putExtra(KEY_PRODUCT_ID, product.getId());
        intent.putExtra(KEY_NAME, product.getName());
        intent.putExtra(KEY_MAIN_IMAGE, product.getMainImage());
        intent.putExtra(KEY_DETAIL, product.getDetail());
        AppUtil.openActivity(intent);
    }

    @Override
    public void onRefresh(boolean isPullDown) {
    }

    @Override
    public void onLoadMore(boolean isSilence) {
        if (isHasNextPage) {
            pageNum++;
            listProduct(null, null);
        } else {
            ToastUtils.showToast(iProductListView.getContext(), "当前已经是最后一页了");
            iProductListView.stopLoadMore();
        }

    }
}
